package com.lx.algorithm.temp;

import java.util.Arrays;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-11-14 16:47:37
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-11-14     张李鑫                     1.0         1.0 Version
 */
public class MLS {


    public static int MLS(int[] arr) {
        // write code here
        Arrays.sort(arr);

        int left = arr.length / 2;
        int right = arr.length / 2;
        int leftMax = 0;
        int rightMax = 0;
        int lAndR = 1;

        boolean flag = true;
        boolean lFlag = false;
        boolean rFlag = false;
        while (left >= 0 || right < arr.length - 1) {
            if (flag) {

                if ((left >= 0 )&&arr[left + 1]  == arr[left]+1) {
                    left--;
                    lAndR++;
                } else {
                    rFlag = true;
                }
                if (( right < arr.length - 1)&&arr[right] + 1 == arr[right - 1]) {
                    right++;
                    lAndR++;
                } else {
                    lFlag = true;
                }
                flag = !(rFlag && lFlag);
            } else {
                if ( (left >= 0 )&& arr[left+1]==arr[left--] + 1) {
                    leftMax++;
                    lAndR = Math.max(leftMax, lAndR);
                } else {

                    leftMax = 1;
                }
                if ((right < arr.length - 1)&&arr[right] + 1 == arr[right++]) {
                    rightMax++;
                    lAndR = Math.max(rightMax, lAndR);
                } else {

                    rightMax = 1;
                }
            }
        }
        return lAndR;
    }

    public static void main(String[] args) {
        int[] arr = {100, 4, 200, 1, 3, 2};
        System.out.println(MLS(arr));
    }
}